<?php

namespace App\Http\Controllers\API\Admin;

use App\Http\Requests\CreateCategoryRequest;
use App\Http\Requests\UpdateCategoryRequest;
use App\Models\Category;
use App\Models\Config;
use App\Models\Goods;
use App\Models\Navigate;
use App\Repositories\CategoryRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\ConfigRepository;
use App\Repositories\NavigateRepository;
use App\Repositories\PicturesRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Facades\Auth;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class ConfigAPIAdminController extends ApiBaseController
{
    private $shop;
    private $configRepository;
    protected $model_name = 'App\Models\Config';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
        'type', 'group', 'title'
    ];

    public function __construct(ConfigRepository $configRepository)
    {
        $this->configRepository = $configRepository;
    }


    public function authLimit()
    {
        $shop = Auth::guard('admin')->user()->shop;
        $limit = [];
        $limit[] = ['shop_id', $shop->id];
        return $limit;
    }

    public function beforeStore($id, $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $model = $this->getModel();
        $obj = $model->find($id);
        $obj->shop_id = $shop->id;
        $obj->save();
        return responseZK(1,$obj);
    }

    public function beforeUpdate($id, $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $model = $this->getModel();
        $obj = $model->find($id);
        $obj->shop_id = $shop->id;
        $obj->save();
        return responseZK(1,$obj);
    }


    public function getConfigsByGroup($group='system')
    {
        if(!$this->shop)
            $this->shop = get_shop();
        $shop_id = $this->shop->id;
        if(!$shop_id){
            $shop = Auth::guard('admin')->user()->shop;
            $shop_id = $shop->id;
        }
        $goods_arr = Goods::with('attributesByStatus')->get()->keyBy('id')->toArray();
        $data = $this->configRepository->getMapByGroup($group,$shop_id);
        if(isset($data['index_layout'])){
            $other = Config::where('type', 'like', "index_layout_%")->where('shop_id',$shop_id)->get();
            $other_str = '';
            foreach ($other as $item) {
                $other_str.=$item->value;
            }
            $data['index_layout'] = json_decode($other_str.$data['index_layout'],true);
//            foreach ($data['index_layout'] as &$v) {
//                foreach ($v['list'] as &$vv) {
//                    if(isset($vv['good']) && isset($goods_arr[$vv['good']])){
//                        $goods_arr[$vv['good']]['attributes'] = $goods_arr[$vv['good']]['attributes_by_status'];
//                        $__ar = $goods_arr[$vv['good']];
//                        unset($__ar['attributes_by_status']);
//                        $vv['goods'] = $__ar;
//                    }
//                }
//            }
        }
//        if(isset($data['index_layout_test']) && \request('is_test')){
//            $other = Config::where('type', 'like', "index_layout_test_%")->where('shop_id',$shop_id)->get();
//            $other_str = '';
//            foreach ($other as $item) {
//                $other_str.=$item->value;
//            }
//            $data['index_layout_test'] = json_decode($other_str.$data['index_layout_test'],true);
//            foreach ($data['index_layout_test'] as &$v) {
//                foreach ($v['list'] as &$vv) {
//                    if(isset($vv['good']) && isset($goods_arr[$vv['good']])){
//                        $goods_arr[$vv['good']]['attributes'] = $goods_arr[$vv['good']]['attributes_by_status'];
//                        $__ar = $goods_arr[$vv['good']];
//                        unset($__ar['attributes_by_status']);
//                        $vv['goods'] = $__ar;
//                    }
//                }
//            }
//        }
        return responseZK(1,$data,'成功');
    }


    public function advertisingSettingStore(Request $request)
    {
        if(!$this->shop)
            $this->shop = get_shop();
        $shop_id = $this->shop->id;
        if(!$shop_id){
            $shop = Auth::guard('admin')->user()->shop;
            $shop_id = $shop->id;
        }
        $data = [
            'img'=>$request->img,
            'time'=>$request->time,
        ];
        $config = Config::where('shop_id',$shop_id)->where('type', 'index_advertising')->update(['value'=>json_encode($data,true)]);

        return responseZK(1,$data,'成功');
    }


    public function settingByType(Request $request)
    {
        if(!$this->shop)
            $this->shop = get_shop();
        $shop_id = $this->shop->id;
        if(!$shop_id){
            $shop = Auth::guard('admin')->user()->shop;
            $shop_id = $shop->id;
        }
        $type = \request('type');
        $value = \request('value');
//        $data = [
//            'img'=>$request->img,
//            'time'=>$request->time,
//        ];
        $config = Config::where('shop_id',$shop_id)->where('type', $type)->first();

        if($config){
            $config->value = $value;
            $config->save();
        }else{
            $config = Config::create([
                'shop_id'=>$shop_id,
                'type'=>$type,
                'value'=>$value,
                'title'=>request('title'),
                'group'=>request('group'),
            ]);
        }
        return responseZK(1,$config,'成功');
    }

    public function setIndexLayout()
    {
        if(!$this->shop)
            $this->shop = get_shop();
        $config = Config::where('type','index_layout')->where('shop_id',$this->shop->id)->first();
        $json = \request('json');
        $other = [];

        while (true){
            $len = strlen($json);
            if($len>60000){
                $_other = substr($json,count($other)*60000,60000);
                $json = substr($json,60000);
                $other[] = $_other;
            }else{
                break;
            }
        }

        Config::where('type', 'like', "index_layout_%")->where('shop_id',$this->shop->id)->delete();
        if(count($other)>0) {
            foreach ($other as $k => $v) {
                Config::create([
                    'group' => 'layout',
                    'shop_id' => $this->shop->id,
                    'type' => 'index_layout_' . $k,
                    'value' => $v,
                    'title' => '布局设置副本',
                ]);
            }
        }
        $config->value = $json;
        $config->save();
        return responseZK(1,$config,'成功');
    }
}
